﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;

namespace Bus_proj.website
{
    public partial class result_phone : System.Web.UI.Page
    {
        Select SData = new Select();
        int seat_ID;
        string[] seat_id = new string[100];
        float charges = 0, tax = 0;
        int chkk = 0;
        float promontion = 0;
        int promotion_id;
        int around_id,ttt=0;
        string payment,ticket;
        float sum;
        string sum2;
        DataClasses1DataContext db = new DataClasses1DataContext();
        public DateTime ConvestToDate(string s)
        {
            int day = 1;
            int mount = 1;
            int years = 1111;
            try
            {
                string[] stemp = s.Split('-');
                years = Convert.ToInt32(stemp[0]);
                mount = Convert.ToInt32(stemp[1]);
                day = Convert.ToInt32(stemp[2]);
                if (years < 2300)
                {
                    years = years + 543;
                }
                return new DateTime(years, mount, day);
            }
            catch (Exception)
            {

            }
            return new DateTime(years, mount, day);
        }
        public static string getMd5Hash(string input)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string strDateNowBooking = Request.Form["strDateNowBooking"];
                string strDateBooking = Request.Form["strDateBooking"];
                string strTime = Request.Form["strTime"];
                string strPoint_id = Request.Form["strPoint_id"];
                string strSeat = Request.Form["strSeat"];
                string strmemberID = Request.Form["strmemberID"];
                string strRoute_id = Request.Form["strRoute_id"];

                //string Around_date_chk = Request.Form["Around_date_chk"].ToString();
                //Response.Write(strDateNowBooking + " " + strDateBooking + " " + strTime + " " + strPoint_id + " " + strSeat + " " + strmemberID + " " + strRoute_id);
                //string Str_chk = "2014-02-16 20:27:30 2014-2-27 10 6 [3C, 4C] 10<!DOCTYPE html PUB";

                //string strDateNowBooking = "2014-02-18 17:05:14";
                //string strDateBooking = "2014-2-28";
                //string strTime = "10";
                //string strPoint_id = "6";
                //string strSeat = "[4C]";
                //string strmemberID = "10";
                //string strRoute_id = "2";

                int strRoute_id_int = Int32.Parse(strRoute_id);
                strSeat = strSeat.Replace(" ", "");
                strSeat = strSeat.Replace("[", "");
                strSeat = strSeat.Replace("]", "");
                string[] stemp = strSeat.Split(',');
                for (int i = 0; i < stemp.Length; i++)
                {
                    string chk = @"select *
                        from Table_Around
                        inner join Table_Route
                        on Table_Around.Route_id = Table_Route.Route_id
                        inner join Table_Sale
                        on Table_Sale.Around_id = Table_Around.Around_id
                        inner join Table_SaleDetail
                        on Table_Sale.Sale_id = Table_SaleDetail.Sale_id
                        where Table_Route.Route_id = " + strRoute_id_int + " and Table_Around.Around_date_chk = '" + strDateBooking + "' and Table_SaleDetail.Seat_no = '" + stemp[i] + "'and (Table_Sale.SSID =1 or Table_Sale.SSID =2 )";
                    string promotion = @"select *
                                    from Table_Promotion
                                    inner join Table_Promotion_round
                                        on Table_Promotion.Promotion_id = Table_Promotion_round.Promotion_id
                                    inner join Table_Route
                                        on Table_Promotion_round.Route_id = Table_Route.Route_id
                                    where Table_Promotion_round.Route_id = " + strRoute_id_int + " and Table_Promotion.Promotion_status = 1";
                    string aroundd = @"select *
                                    from Table_Around
                                    inner join Table_Route
                                    on Table_Around.Route_id = Table_Route.Route_id
                                    where Table_Route.Route_id = " + strRoute_id_int + " and Table_Around.Around_date_chk = '" + strDateBooking + "' and Time_id =" + Int32.Parse(strTime) + "";
                    SqlCommand sqlcomm = new SqlCommand();
                    string Connection = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                    SqlConnection sqlconn = new SqlConnection(Connection);
                    SqlDataAdapter da = new SqlDataAdapter(chk, sqlconn);
                    SqlDataAdapter da2 = new SqlDataAdapter(promotion, sqlconn);
                    SqlDataAdapter da3 = new SqlDataAdapter(aroundd, sqlconn);
                    DataTable dt = new DataTable();
                    DataTable dt2 = new DataTable();
                    DataTable dt3 = new DataTable();
                    da.Fill(dt);
                    da2.Fill(dt2);
                    da3.Fill(dt3);
                    if (dt.Rows.Count <= 0)
                    {
                        chkk = 0;
                        //around_id = Int32.Parse(dt3.Rows[0]["Around_id"].ToString());
                        //around_id = Int32.Parse(dt.Rows[0]["Around_id"].ToString());
                    }
                    else
                    {
                        chkk++;
                    }
                    if (dt2.Rows.Count <= 0)
                    {
                        promontion = 0;
                    }
                    else
                    {
                        promontion = float.Parse(dt2.Rows[0]["Promotion_discount"].ToString());
                        promotion_id = Int32.Parse(dt2.Rows[0]["Promotion_id"].ToString());
                    }
                }
                if (strRoute_id.Length == 1)
                {
                    strRoute_id = "00" + strRoute_id;
                }
                else if (strRoute_id.Length == 2)
                {
                    strRoute_id = "0" + strRoute_id;
                }
                if (chkk == 0)
                {
                    string aroundd2 = @"select *
                                from Table_Around
                                inner join Table_Route
                                    on Table_Around.Route_id = Table_Route.Route_id  
                                inner join Table_Point
                                    on Table_Point.Route_id =  Table_Route.Route_id                       
                                where Table_Route.Route_id = " + strRoute_id_int + " and Table_Around.Around_date_chk = '" + strDateBooking + "' and Time_id =" + Int32.Parse(strTime) + "";
                    SqlCommand sqlcomm2 = new SqlCommand();
                    string Connection2 = ConfigurationManager.ConnectionStrings["Bus_proConnectionString"].ConnectionString;
                    SqlConnection sqlconn2 = new SqlConnection(Connection2);
                    SqlDataAdapter da_4 = new SqlDataAdapter(aroundd2, sqlconn2);
                    DataTable dt_4 = new DataTable();
                    da_4.Fill(dt_4);
                    around_id = Int32.Parse(dt_4.Rows[0]["Around_id"].ToString());
                    charges = float.Parse(dt_4.Rows[0]["Point_charges"].ToString());
                    tax = float.Parse(dt_4.Rows[0]["Point_tax"].ToString());
                    float ans_discount = (charges * (float)(5 + promontion)) / 100;
                    float sum = (float)stemp.Length * ((charges - ans_discount) + tax);
                    sum2 = sum.ToString();
                    ticket = SData.GetId_tickettt();
                    payment = "896" + ticket;
                    for (int i = 0; i < stemp.Length; i++)
                    {
                        try
                        {
                            Table_Sale sale_save = new Table_Sale();
                            ticket = SData.GetId_tickettt();
                            sale_save.Sale_id = ticket;
                            //Response.Write(901);
                            sale_save.Passenger_id = Int32.Parse(strmemberID);
                            //Response.Write(902);
                            //DateTime a = ConvestToDate(DateTime.Now.ToShortDateString());
                            sale_save.Sale_date = ConvestToDate(strDateBooking);
                            // Response.Write(903);
                            string date_time = DateTime.Now.ToString("HH:mm:ss");
                            sale_save.Sale_time = DateTime.Parse(date_time);
                            //Response.Write(904);
                            sale_save.Promotion_id = promotion_id;
                            //Response.Write(905);
                            sale_save.Point_id = Int32.Parse(strPoint_id);
                            //Response.Write(906);
                            sale_save.Around_id = around_id;
                            //Response.Write(907);
                            sale_save.SSID = 2;
                            //Response.Write(908);
                            //sale_save.Personnel_id = "";
                            sale_save.percentage = 100;
                            db.Table_Sales.InsertOnSubmit(sale_save);
                            db.SubmitChanges();
                            //Response.Write(134);
                            Table_SaleDetail seat = new Table_SaleDetail();
                            seat_ID = SData.GetId_seat();
                            seat.Seat_id = seat_ID;
                            seat.Seat_no = stemp[i];
                            seat.Sale_id = ticket;
                            seat.Price_ticket = charges;
                            seat.tex = tax;
                            seat.Around_id = around_id;
                            db.Table_SaleDetails.InsertOnSubmit(seat);
                            db.SubmitChanges();
                            //Response.Write(456);

                            Table_payment pay = new Table_payment();
                            pay.payment_no = SData.GetId_pay();
                            pay.payment_id = payment;
                            pay.payment_datetime = DateTime.Parse(DateTime.Now.ToString("HH:mm:ss"));
                            pay.Sale_id = ticket;
                            pay.payment_status = false;
                            pay.payment_enddate = DateTime.Parse(DateTime.Now.ToString("HH:mm:ss")).AddHours(12);
                            //pay.payment_count = count;
                            pay.encode = getMd5Hash(payment);
                            db.Table_payments.InsertOnSubmit(pay);
                            db.SubmitChanges();
                        }
                        catch (Exception)
                        {
                            ttt++;
                            Response.Write(0 + " ");
                        }
                    }
                }
                else
                {
                    ttt++;
                    Response.Write(0 + " ");
                }
                if (ttt == 0)
                {
                    Response.Write(1 + " " + payment + " " + sum2 + " ");
                }
            }
            catch (Exception)
            {
                Response.Write(3 + " ");
            }       
            
        }
    }
}